package com.js.basicpage.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.js.basicpage.R;
import com.js.basicutils.DisplayUtil;


/**
 * 加载框
 */
public class LoadingDialog extends Dialog {

	private Context context = null;

	private TextView tvMsg;
	private ImageView ivloading;

	public LoadingDialog(Context context) {
		super(context);
		this.context = context;
		init();
	}

	public LoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		this.context = context;
		init();
	}

	public LoadingDialog(Context context, int theme) {
		super(context, theme);
		this.context = context;
		init();
	}

	private void init() {
		// 加载自己定义的布局
		View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
		ivloading = (ImageView) view.findViewById(R.id.iv_loading);
		tvMsg = (TextView) view.findViewById(R.id.tv_msg);

		// 加载XML文件中定义的动画
		Drawable drawable = ivloading.getDrawable();
		if (drawable != null && drawable instanceof AnimationDrawable) {
			AnimationDrawable animationDrawable = (AnimationDrawable) drawable;
			animationDrawable.start();
		}

		// 为Dialoge设置自己定义的布局
		setContentView(view);

		WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); // 获取对话框当前的参数值
		layoutParams.width = DisplayUtil.dip2px(getContext(), 112);
		layoutParams.height = DisplayUtil.dip2px(getContext(), 112);

		// layoutParams.y = d.getHeight() / 4;
		getWindow().setAttributes(layoutParams);
	}

	public void setMessage(String msg) {
		if (null != tvMsg) {
			tvMsg.setText(msg);
		}
	}

	public void setMessage(int resId) {
		if (null != tvMsg) {
			tvMsg.setText(context.getString(resId));
		}
	}

}
